package logic

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
	"library/library/internal/svc"
	"library/library/library"
	"library/models/mysql"
)

type PrefaceLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPrefaceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PrefaceLogic {
	return &PrefaceLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PrefaceLogic) Preface(in *library.PrefaceRequest) (*library.PrefaceResponse, error) {
	// todo: add your logic here and delete this line

	user := mysql.User{
		Model:       gorm.Model{ID: uint(in.UserId)},
		Name:        in.Name,
		HeaderImage: in.HeaderImage,
	}
	err := mysql.DB.Debug().Updates(&user).Error
	if err != nil {
		return nil, fmt.Errorf("完善信息失败")
	}

	return &library.PrefaceResponse{UserId: int64(user.ID)}, nil
}
